package vn.co.thn.web.demo_app_engine.bean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class BeanBase {
	@SuppressWarnings("unchecked")
	public static <T> T RequestToObject(HttpServletRequest request,Class<T> clazz) throws Exception{
		Object object = clazz.getConstructor().newInstance();
		List<Field> fields = new ArrayList<Field>();
		fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
		Enumeration<?> e = request.getParameterNames();
		while (e.hasMoreElements()) {
			String ParameterNames = "";
			ParameterNames = (String) e.nextElement();
			for (Field field : fields) {
				field.setAccessible(true);
				if(field.getName().equalsIgnoreCase(ParameterNames)){
					field.set(object,request.getParameter(ParameterNames));
				}
			}
		}
	
		return (T) object;
	}
}
